/**
 * 智慧小区管理系统 - 维修服务API接口
 * @author Wu.Liang
 * @date 2024-12-21
 */

import api from '@/api/request'
import type {
  ApiResponse,
  PageResponse,
  BaseQueryParams,
  RepairOrder,
  RepairProgress,
  CreateOrderParams,
  AssignOrderParams,
  CompleteOrderParams,
  RepairOrderStatistics
} from '@/views/property/types/repair'

// 维修工单API
export const repairOrderApi = {
  // 分页查询维修工单
  getRepairOrderList: (params: any) => {
    return api.get('/api/property/repair-orders', { params })
  },

  // 创建维修工单
  createRepairOrder: (data: any) => {
    return api.post('/api/property/repair-orders', data)
  },

  // 获取工单详情
  getRepairOrderDetail: (orderId: number) => {
    return api.get<ApiResponse<RepairOrder>>(`/api/property/repair-orders/${orderId}`)
  },

  // 分配维修工单
  assignRepairOrder: (orderId: number, data: any) => {
    const params = new URLSearchParams()
    params.append('repairmanId', data.repairmanId.toString())
    if (data.remark) {
      params.append('remark', data.remark)
    }
    return api.put(`/api/property/repair-orders/${orderId}/assign?${params.toString()}`)
  },

  // 更新工单状态
  updateRepairOrderStatus: (orderId: number, status: number, remark?: string) => {
    return api.put(`/api/property/repair-orders/${orderId}/status?status=${status}`, {
      remark
    })
  },

  // 完成工单
  completeRepairOrder: (orderId: number, data: any) => {
    const params = new URLSearchParams()
    params.append('result', data.result)
    if (data.remark) {
      params.append('remark', data.remark)
    }
    return api.put(`/api/property/repair-orders/${orderId}/complete?${params.toString()}`)
  },

  // 取消工单
  cancelRepairOrder: (orderId: number, reason: string) => {
    return api.put(`/api/property/repair-orders/${orderId}/cancel?reason=${encodeURIComponent(reason)}`)
  },

  // 获取维修工单统计信息
  getRepairOrderStatistics: (params: { communityId: number; startDate?: string; endDate?: string }) => {
    return api.get<ApiResponse<RepairOrderStatistics>>('/api/property/repair-orders/statistics', { params })
  },

  // 批量分配工单
  batchAssignOrders: (data: { orderIds: number[]; repairmanId: number }) => {
    return api.post<ApiResponse<{ successCount: number; totalCount: number }>>('/api/property/repair-orders/batch-assign', null, {
      params: {
        orderIds: data.orderIds.join(','),
        repairmanId: data.repairmanId
      }
    })
  },

  // 删除工单
  deleteRepairOrder: (orderId: number) => {
    return api.delete<ApiResponse<boolean>>(`/api/property/repair-orders/${orderId}`)
  },

  // 批量删除工单
  batchDeleteOrders: (data: { orderIds: number[] }) => {
    return api.post<ApiResponse<{ successCount: number; totalCount: number }>>('/api/property/repair-orders/batch-delete', {
      orderIds: data.orderIds
    })
  }
}

// 维修工单进度API
export const repairProgressApi = {
  // 根据工单ID查询进度记录列表
  getProgressListByOrderId: (orderId: number) => {
    return api.get<ApiResponse<RepairProgress[]>>('/api/property/repair-progress/list', {
      params: { orderId }
    })
  },

  // 根据工单号查询进度记录列表
  getProgressListByOrderNo: (orderNo: string) => {
    return api.get<ApiResponse<RepairProgress[]>>('/api/property/repair-progress/list-by-order-no', {
      params: { orderNo }
    })
  },

  // 获取工单最新进度记录
  getLatestProgressByOrderId: (orderId: number) => {
    return api.get<ApiResponse<RepairProgress>>('/api/property/repair-progress/latest', {
      params: { orderId }
    })
  },

  // 统计工单进度数量
  countProgressByOrderId: (orderId: number) => {
    return api.get<ApiResponse<number>>('/api/property/repair-progress/count', {
      params: { orderId }
    })
  },

  // 根据进度类型查询进度记录
  getProgressByOrderIdAndType: (orderId: number, progressType: number) => {
    return api.get<ApiResponse<RepairProgress>>('/api/property/repair-progress/by-type', {
      params: { orderId, progressType }
    })
  },

  // 查询时间范围内的进度记录
  getProgressListByTimeRange: (params: { startTime?: string; endTime?: string }) => {
    return api.get<ApiResponse<RepairProgress[]>>('/api/property/repair-progress/by-time-range', {
      params
    })
  },

  // 查询维修人员相关的进度记录
  getProgressListByRepairmanId: (params: { repairmanId: number; startTime?: string; endTime?: string }) => {
    return api.get<ApiResponse<RepairProgress[]>>('/api/property/repair-progress/by-repairman', {
      params
    })
  },

  // 手动创建进度记录
  createProgress: (progress: Partial<RepairProgress>) => {
    return api.post<ApiResponse<boolean>>('/api/property/repair-progress/create', progress)
  },

  // 简单创建进度记录
  createSimpleProgress: (params: {
    orderId: number
    orderNo: string
    progressType: number
    progressTitle: string
    progressContent: string
    operatorId?: number
    operatorName?: string
  }) => {
    return api.post<ApiResponse<boolean>>('/api/property/repair-progress/create-simple', null, {
      params
    })
  }
}

// 通用API
export const commonApi = {
  // 获取小区列表
  getCommunityList: () => {
    return api.get('/api/region/communities/list')
  },

  // 获取楼栋列表
  getBuildingList: (params: { communityId: number }) => {
    return api.get<ApiResponse<any[]>>('/api/region/buildings', { params })
  },

  // 获取房户列表
  getHouseholdList: (params: { communityId?: number; buildingId?: number }) => {
    return api.get<ApiResponse<any[]>>('/api/region/households', { params })
  },

  // 获取维修人员列表
  getRepairmanList: (params: { communityId?: number; status?: number }) => {
    return api.get<ApiResponse<any[]>>('/api/property/repairmen', { params })
  },

  // 获取用户列表（用于分配）
  getUserList: (params: { communityId?: number; roleType?: string }) => {
    return api.get<ApiResponse<any[]>>('/api/user/list', { params })
  }
} 